package com.example.demo.service.impl;

import com.example.demo.dao.SceneryDao;
import com.example.demo.entity.Scenery;
import com.example.demo.service.SceneryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SceneryServiceImpl implements SceneryService {
    private SceneryDao sceneryDao;

    @Override
    public List<Scenery> getAll() {
        return sceneryDao.getAll();
    }

    @Override
    public int addScenery(Scenery scenery) {
        return sceneryDao.addScenery(scenery);
    }

    @Override
    public int deleteScenery(Integer id) {
        return sceneryDao.deleteScenery(id);
    }

    @Override
    public int updateScenery(Scenery scenery) {
        return sceneryDao.updateScenery(scenery);
    }

    @Override
    public Scenery findSceneryById(Integer id) {
        return sceneryDao.findSceneryById(id);
    }

    @Override
    public PageInfo<Scenery> find(String sceneryCity, String sceneryTitle, Double lowerPrice,Double HighPrice,int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize) ;
        Map<String,Object> map = new HashMap<>() ;
        if(sceneryCity != null && !sceneryCity.equals("")){
            map.put("sceneryCity","%"+sceneryCity+"%") ;
        }
        if(sceneryTitle!= null && !sceneryTitle.equals("")){
            map.put("sceneryTitle","%"+sceneryTitle+"%") ;
        }
        map.put("lowPrice",lowerPrice) ;
        map.put("highPrice",HighPrice) ;
        System.out.println(map);
        List<Scenery> list=sceneryDao.find(map);
        PageInfo<Scenery> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Scenery> getPage(int pageNo, int pageSize) {
        //开始分页
        PageHelper.startPage(pageNo,pageSize) ;
        //获取所有数据
        List<Scenery> list = sceneryDao.getAll() ;
        //创建PageInfo对象
        PageInfo<Scenery> pageInfo = new PageInfo<>(list) ;

        return pageInfo;
    }

    @Autowired
    public void setSceneryDao(SceneryDao sceneryDao) {
        this.sceneryDao = sceneryDao;
    }
}
